//
//  MAFunctionCell.m
//  meiang_edu
//
//  Created by Liubi_Chaos_G on 2018/8/1.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MAFunctionCell.h"
#import "MAFuncGroupModel.h"
#import "MAHomeWatchCell.h"

#import "MAMemberMgrController.h"
#import "MAFamilyEditController.h"
#import "MAJiGouModel.h"
//#import "WGQAttendanceRecordsViewController.h"
#import "WGQTransfersRecordsViewController.h"
#import "WGQAttendanceRecordsViewController.h"
#import "MAFollowMgrViewController.h"
#import "MAZhuanShuServiceController.h"
#import "MAPhotoListViewController.h"
#import "WGQGrowUpLittleViewController.h"
#import "WGQDietRecordsViewController.h"

#import "MAStudentEditController.h"//添加宝宝
#import "MAScanViewController.h"//扫一扫
#import "MANavigationController.h"
#import "MAHomeWorkEditFinishVC.h"//通用录入
#import "MAAuthorViewController.h"

#import "MANoteManageViewController.h"//消息管理
#import "MACookManageViewController.h"//食谱管理

#define FunctionCell @"FunctionCell"
#define FunctionItemCell @"FunctionItemCell"
#define ItemW ((SCREEN_WIDTH - 48) / 4)
#define ItemH (IS_IPHONE_5 ? (ItemW + 20) : ItemW)
#define Cols 4
#define Margin 12

@interface MAFunctionCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *functionTitleLabel;
@property (weak, nonatomic) IBOutlet UIView *imgsContainerView;
@property (weak, nonatomic) IBOutlet UIView *bgView;



@end

@implementation MAFunctionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.bgView.layer.cornerRadius = 4;
    self.bgView.layer.masksToBounds = YES;
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
    }
    return self;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    MAFunctionCell *cell = [tableView dequeueReusableCellWithIdentifier:FunctionCell];
    if (cell == nil) {
        cell = [[MAFunctionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FunctionCell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)setGroupModel:(MAFuncGroupModel *)groupModel {
    _groupModel = groupModel;
    self.functionTitleLabel.text = groupModel.group_name;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,groupModel.image]]];
    // 九宫格
    NSInteger index = groupModel.items.count;
    for (int i=0; i<index; i++) {
        MAHomeWatchCell *img = [[MAHomeWatchCell alloc] init];
        img.tag = i;
        img.width = ItemW;
        img.height = ItemH;
        // 小格子x坐标
        NSInteger tempX = i % Cols;  // 小格子所在的列
        CGFloat bagX = tempX * ItemW;
        // 小格子y坐标
        NSInteger tempY = i / Cols; // 小格子所在的行
        CGFloat bagY = tempY * ItemH;
        img.x = bagX;
        img.y = bagY;
        [self.imgsContainerView addSubview:img];
        img.funcItemModel = groupModel.items[i];
        img.jigouModel = self.model;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgDidClick:)];
        [img addGestureRecognizer:tap];
    }
    groupModel.cellHeight = ceil(index * 1.0 / Cols) * ItemH + 40 + 5;
}

- (void)imgDidClick:(UITapGestureRecognizer *)tap {
    DLog(@"%@",tap.view);
    
    MAFuncItemModel *funcItemModel = self.groupModel.items[tap.view.tag];
    MAHomeWatchCell *cell = (MAHomeWatchCell *)tap.view;
    DLog(@"fieldname=%@",funcItemModel.fieldname);
    if ([cell.nameLabel.text isEqualToString:@"接送人管理"])
    {
        MAMemberMgrController *vc = [[MAMemberMgrController alloc] init];
        vc.title = @"接送人管理";
        vc.type = MAMemberMgrControllerTypeUserMgr;
        vc.funcItemModel = funcItemModel;
        vc.jigouModel = self.model;
        vc.jigou_id = self.jigou_id;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }
    else if ([cell.nameLabel.text isEqualToString:@"查看食谱"])
    {
        [MBManager showBriefAlert:@"此功能尚未开通，敬请期待……"];
    }
    else if ([cell.nameLabel.text isEqualToString:@"食谱管理"])
    {
        MACookManageViewController * vc = [MACookManageViewController new];
        vc.jigouModel = self.model;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }
    else if ([cell.nameLabel.text isEqualToString:@"发送专属服务"])
    {
        MAHomeWorkEditFinishVC *vc = [[MAHomeWorkEditFinishVC alloc]init];
        MAFuncItemModel *funcItemModel = self.groupModel.items[tap.view.tag];
        vc.giscene = funcItemModel.function_code;
        vc.or_id = self.model.ID;
//        vc.keyid = self.keyid;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }
    else if ([cell.nameLabel.text isEqualToString:@"添加宝宝"])
    {
        MAStudentEditController *vc = [[UIStoryboard storyboardWithName:NSStringFromClass([MAStudentEditController class]) bundle:nil] instantiateInitialViewController];
        vc.title = @"添加宝宝";
        vc.jigouModel = self.model;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }
    else if ([cell.nameLabel.text isEqualToString:@"家庭成员"]) {
        DLog(@"");
        MAMemberMgrController *vc = [[MAMemberMgrController alloc] init];
        vc.title = @"家庭成员";
        vc.type = MAMemberMgrControllerTypeFamily;
        vc.funcItemModel = funcItemModel;
        vc.jigou_id = self.jigou_id;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    } else if ([cell.nameLabel.text isEqualToString:@"宝宝管理"]) {
        MAMemberMgrController *vc = [[MAMemberMgrController alloc] init];
        vc.title = @"宝宝管理";
        vc.type = MAMemberMgrControllerTypeBaby;
        vc.funcItemModel = funcItemModel;
        vc.jigou_id = self.jigou_id;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    } else if ([cell.nameLabel.text isEqualToString:@"家庭详情"]) {
        MAFamilyEditController *vc = [[UIStoryboard storyboardWithName:NSStringFromClass([MAFamilyEditController class]) bundle:nil] instantiateInitialViewController];
        vc.title = @"详情编辑";
        vc.jigouModel = self.model;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    } else if ([cell.nameLabel.text isEqualToString:@"详情编辑"]) { // 修改机构信息
        MAFamilyEditController *vc = [[UIStoryboard storyboardWithName:NSStringFromClass([MAFamilyEditController class]) bundle:nil] instantiateInitialViewController];
        vc.title = @"详情编辑";
        vc.jigouModel = self.model;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    } else if ([cell.nameLabel.text isEqualToString:@"安排接送"]) { // 安排接送
        MAMemberMgrController *vc = [[MAMemberMgrController alloc] init];
        vc.title = @"安排接送";
        vc.type = MAMemberMgrControllerTypeJieSong;
        vc.funcItemModel = funcItemModel;
        vc.jigouModel = self.model;
        vc.jigou_id = self.jigou_id;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    } else if ([cell.nameLabel.text isEqualToString:@"接送记录"]) { // 接送记录
        WGQTransfersRecordsViewController * vc = [[WGQTransfersRecordsViewController alloc] init];
        vc.funcItemModel = funcItemModel;
        vc.jigouModel = self.model;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    } else if ([cell.nameLabel.text isEqualToString:@"请假考勤"]){
        WGQAttendanceRecordsViewController * vc = [[WGQAttendanceRecordsViewController alloc] init];
        vc.funcItemModel = funcItemModel;
        vc.jigouModel = self.model;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    } else if ([cell.nameLabel.text isEqualToString:@"关注管理"]) { // 关注管理
        MAFollowMgrViewController * vc = [[MAFollowMgrViewController alloc] init];
        vc.jigouModel = self.model;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }
    else if ([cell.nameLabel.text isEqualToString:@"专属服务记录"]) { // 专属服务记录
        MAZhuanShuServiceController * vc = [[MAZhuanShuServiceController alloc] init];
        vc.jigouModel = self.model;
        vc.funcItemModel = funcItemModel;
        vc.title = @"专属服务记录";
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }
    else if ([cell.nameLabel.text isEqualToString:@"专属服务管理"]) { // 专属服务管理
        MAZhuanShuServiceController * vc = [[MAZhuanShuServiceController alloc] init];
        vc.jigouModel = self.model;
        vc.funcItemModel = funcItemModel;
        vc.title = @"专属服务管理";
        [self.viewController.navigationController pushViewController:vc animated:YES];
    } else if ([cell.nameLabel.text isEqualToString:@"家庭相册"]) { // 家庭相册
        MAPhotoListViewController * vc = [[MAPhotoListViewController alloc] init];
        vc.jigouModel = self.model;
        vc.funcItemModel = funcItemModel;
        vc.title = @"家庭相册";
        [self.viewController.navigationController pushViewController:vc animated:YES];
    } else if([cell.nameLabel.text isEqualToString:@"饮食记录"]){
        WGQDietRecordsViewController * vc = [[WGQDietRecordsViewController alloc] init];
        vc.funcItemModel = funcItemModel;
        vc.jigouModel = self.model;
        [self.viewController.navigationController pushViewController:vc animated:YES];
       
    } else if([cell.nameLabel.text isEqualToString:@"点滴记录"]){
        WGQGrowUpLittleViewController * vc = [[WGQGrowUpLittleViewController alloc] init];
        vc.funcItemModel = funcItemModel;
        vc.jigouModel = self.model;
        [self.viewController.navigationController pushViewController:vc animated:YES];
        
    } else if([cell.nameLabel.text isEqualToString:@"添加家庭成员"] || [cell.nameLabel.text isEqualToString:@"添加接送人"]){
        MAScanViewController *vc = [[MAScanViewController alloc] init];
        MANavigationController *nav = [[MANavigationController alloc] initWithRootViewController:vc];
        MJWeakSelf;
        vc.scanBlock = ^(NSString *scanResult) {
            NSArray *arr = [scanResult componentsSeparatedByString:@","];
            if (arr.count >= 3) {
                MAAuthorViewController *authorVC = [[UIStoryboard storyboardWithName:NSStringFromClass([MAAuthorViewController class]) bundle:nil] instantiateInitialViewController];
                [weakSelf.viewController.navigationController pushViewController:authorVC animated:YES];
                authorVC.scanResult = scanResult;
                authorVC.jigouModel = self.model;
                authorVC.funcItemModel = funcItemModel;
            } else {
                [MBManager showBriefAlert:@"错误的二维码"];
                return;
            }
        };
        [self.viewController.navigationController presentViewController:nav animated:YES completion:nil];
        
    }else if([cell.nameLabel.text isEqualToString:@"通知管理"]||
             [cell.nameLabel.text containsString:@"作业管理"]||
             [cell.nameLabel.text isEqualToString:@"活动管理"]||
             [cell.nameLabel.text isEqualToString:@"动态管理"]||
             [cell.nameLabel.text isEqualToString:@"报名管理"]||
             [cell.nameLabel.text isEqualToString:@"资讯管理"]||
             [cell.nameLabel.text isEqualToString:@"服务管理"]||
             [cell.nameLabel.text isEqualToString:@"招生管理"]||
             [cell.nameLabel.text isEqualToString:@"消息管理"]){
        MANoteManageViewController * vc = [MANoteManageViewController new];
        vc.funcItemModel = funcItemModel;
        vc.title = cell.nameLabel.text;
        vc.jigouModel = self.model;
        vc.jigou_id = self.jigou_id;
        [self.viewController.navigationController pushViewController:vc animated:YES];
    }
}

@end
